//
//  Asset.swift
//  Asset
//
//  Created by 左忠飞 on 2021/8/9.
//

import Foundation

class Asset: CustomStringConvertible {
    let name: String
    let value: Double
    //使用weak弱引用,打破循环引用
    //弱引用必须是var,不能是let
    //弱引用必须声明为可空类型
    //原因:一旦弱引用指向的实例被释放,弱引用就会变成nil,能变成nil的类型只有可控类型,所以弱引用必须是可空的.引用let声明的实例不能变,所以弱引用必须用var声明
    weak var owner: Person?
    
    var description: String {
        if let actualOwner = owner {
            return "Asset(\(name), worth\(value), owned by \(actualOwner)"
        } else {
            return "Asset(\(name), worth\(value), owned by anyone"
        }
    }
    
    init(name:String,value:Double) {
        self.name = name
        self.value = value
    }
    
    deinit {
        print("\(self) is being deallocated")
    }
}
